﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Collections.ObjectModel;
using DCT.AddIn.AddInView;
using System.IO;

namespace DCT.AddIn.DeviceExplorer
{
    /// <summary>
    /// DeviceExplorerView.xaml 的交互逻辑
    /// </summary>
    public partial class DeviceExplorerView : UserControl
    {
        public DeviceExplorerView(XElement xmlData, HostObject hostObject)
        {
            _hostObject = hostObject;
            Init(xmlData);
            InitializeComponent();
            DataContext = this;
        }

        private HostObject _hostObject;
        public List<DevicePropertyNameData> FilterPropertyNameList { get; private set; }
        public List<DeviceExplorerData> DeviceExplorerDataList { get; private set; }
        public ObservableCollection<DeviceExplorerData> DeviceExplorerDatas { get; private set; }

        private void Init(XElement xmlData)
        {
            FilterPropertyNameList = new List<DevicePropertyNameData>();
            DeviceExplorerDataList = new List<DeviceExplorerData>();
            DeviceExplorerDatas = new ObservableCollection<DeviceExplorerData>();
            DevicePropertyNameData devicePropertyNameData = new DevicePropertyNameData("Device Type");

            XElement dsuXMLData = xmlData.Element("DSU");
            if (dsuXMLData == null)
            {
                return;
            }

            foreach (XElement e in dsuXMLData.Elements())
            {
                DeviceExplorerData deviceExplorerData = new DeviceExplorerData(e);
                DeviceExplorerDataList.Add(deviceExplorerData);
                //DeviceExplorerDatas.Add(deviceExplorerData);

                if (devicePropertyNameData.DevicePropertyValueDataList.Select(v => v.PropertyValue).Contains(deviceExplorerData.TypeName))
                {
                    devicePropertyNameData.DevicePropertyValueDataList.First(v => v.PropertyValue == deviceExplorerData.TypeName).DeviceNumber++;
                }
                else
                {
                    devicePropertyNameData.DevicePropertyValueDataList.Add(new DevicePropertyValueData(deviceExplorerData.TypeName));
                }
            }

            FilterPropertyNameList.Add(devicePropertyNameData);
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            DeviceExplorerDatas.Clear();
            foreach (DeviceExplorerData deviceExplorerData in DeviceExplorerDataList.Where(v => Adjudicator(v)))
            {
                deviceExplorerData.Selected = true;
                DeviceExplorerDatas.Add(deviceExplorerData);
            }
        }

        private bool Adjudicator(DeviceExplorerData deviceExplorerData)
        {
            foreach (DevicePropertyNameData propertyName in FilterPropertyNameList)
            {
                if (propertyName.Selected)
                {
                    switch (propertyName.PropertyName)
                    {
                        case "Device Type":
                            if (!propertyName.DevicePropertyValueDataList.Where(v => v.Selected).Select(v1 => v1.PropertyValue).Contains(deviceExplorerData.TypeName))
                            {
                                return false;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }

            return true;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button bt = sender as Button;

            XElement root = new XElement("root");
            XElement dsu = new XElement("DSU");
            root.Add(dsu);

            if (bt != null)
            {
                switch ((string)bt.Content)
                {
                    case "Select":
                        foreach (DeviceExplorerData deviceExplorerData in DeviceExplorerDatas.Where(v => v.Selected))
                        {
                            dsu.Add(GenerateXMLChanges(deviceExplorerData.DeviceID, "IsSelected", true));
                        }
                        break;
                    case "DeSelect":
                        foreach (DeviceExplorerData deviceExplorerData in DeviceExplorerDatas.Where(v => v.Selected))
                        {
                            dsu.Add(GenerateXMLChanges(deviceExplorerData.DeviceID, "IsSelected", false));
                        }
                        break;
                    case "Display":
                        foreach (DeviceExplorerData deviceExplorerData in DeviceExplorerDatas.Where(v => v.Selected))
                        {
                            dsu.Add(GenerateXMLChanges(deviceExplorerData.DeviceID, "IsDisplay", true));
                        }
                        break;
                    case "Hide":
                        foreach (DeviceExplorerData deviceExplorerData in DeviceExplorerDatas.Where(v => v.Selected))
                        {
                            dsu.Add(GenerateXMLChanges(deviceExplorerData.DeviceID, "IsDisplay", false));
                        }
                        break;
                    default:
                        break;;
                }
            }

            ApplyData(root);
        }

        private XElement GenerateXMLChanges(string deviceID, string propertyName, object propertyValue)
        {
            return new XElement("DeviceChange",
                                new XAttribute("DeviceID", deviceID),
                                new XElement(propertyName, propertyValue));
        }

        private void ApplyData(XElement changesData)
        {
            MemoryStream dataStream = new MemoryStream();
            changesData.Save(dataStream);
            dataStream.Flush();
            dataStream.Seek(0, SeekOrigin.Begin);
            _hostObject.ApplyChanges(dataStream);
        }
    }

    public class DeviceExplorerData
    {
        public DeviceExplorerData(XElement xmlData)
        {
            DeviceID = (string)xmlData.Element("DeviceID");
            DeviceName = (string)xmlData.Element("DeviceName");
            TypeName = (string)xmlData.Element("DeviceType");
            //CPID = (string)xmlData.Element("DeviceName");
        }

        public string DeviceID { get; set; }
        public string DeviceName { get; set; }
        public string TypeName { get; set; }
        //public uint CPID { get; set; }
        public bool Selected { get; set; }
    }

    public class DevicePropertyNameData
    {
        public DevicePropertyNameData(string propertyName)
        {
            PropertyName = propertyName;
            Selected = true;
            DevicePropertyValueDataList = new List<DevicePropertyValueData>();
        }

        public string PropertyName { get; private set; }
        public bool Selected { get; set; }
        public List<DevicePropertyValueData> DevicePropertyValueDataList { get; private set; }
    }

    public class DevicePropertyValueData
    {
        public DevicePropertyValueData(string propertyValue)
        {
            PropertyValue = propertyValue;
        }

        public string PropertyValue { get; private set; }
        public int DeviceNumber { get; set; }
        public bool Selected { get; set; }
    }
}
